<?php include '../public/header.php' ?>
<?php include '../public/header_index.php' ?>
<?php include "../public/header_edit.php"?>
<?php include "../public/header_main.php";?>
<style>
    .page-titlesss{position: absolute;right: 0;top:19px}
    .page-titlesss .page-title-opt-list{display: inline-block;margin-left: 10px}
    #commentForm .main-container .layui-form-item{padding: 0 10px!important;}

    #type_content{padding-top: 28px}
    #type_content button{width: 80%;margin-left: 10%!important;margin-top:10px;height: 35px;line-height: 29px;}
    #type_content .active{background-color:#2272eb;color: #FFFFFF}

    #budtable thead tr th{font-size: 14px!important;font-weight: bold}
    #budtable tbody tr td{font-size: 13px!important;}
</style>
<body class="laytp-container" id="optPage">
<div style="height: 45px"></div>
<div id="opt_nav">
</div>
<div id="dialog">
    <div class="page-title" id="page-title">
        <div class="page-title-con">
            <span class="title" id="titss"></span>
            <div class="move_title"></div>
            <div class="page-title-opt-list">
                <span id="page-close" class="page-close" onclick="layer_close()">×</span>
            </div>
        </div>
    </div>
</div>
<?php include "dao.php"?>

<div class="layui-card" id="table-card">
    <div class="layui-card-body">
        <div class="layui-row">
            <div class="layui-col-md2" style="width: 150px!important;">
                <div id="type_content">
                    <button class="layui-btn layui-btn-primary" key="chui" type="button">垂直运输费</button>
                    <button class="layui-btn layui-btn-primary" key="cuo" type="button">措施费</button>
                    <button class="layui-btn layui-btn-primary" key="gong" type="button">管理人员工资</button>
                    <button class="layui-btn layui-btn-primary" key="guan" type="button">管理费</button>
                    <button class="layui-btn layui-btn-primary" key="yuone" type="button">预置表1</button>
                    <button class="layui-btn layui-btn-primary" key="yutwo" type="button">预置表2</button>
                </div>
            </div>
            <div class="layui-col-md10" style="width: calc(100% - 150px)!important;">
                <form class="layui-form layui-form-pane" id="commentForm">
                    <div class="page-titlesss">
                        <div class="page-title-con">
                            <span id="edittit"></span>
                            <div class="page-title-opt-list">
                                <button type="button" class="layui-btn layui-btn-sm btn-dark marginr5" lay-submit="" key="temp" lay-filter="add">保存</button>
                            </div>
                        </div>
                    </div>
                    <div class="main-container" style="padding: 0!important;margin: 0!important;">

                        <div class="block-generic" style="margin-top: 0!important;">
                            <div class="layui-form-item height_auto">
                                <div id="editshi" class="float-r colorlan"></div>
                                <div class="budopt"></div>
                                <div class="budbox" >
                                    <table id="budtable" class="tablewid" openkey="0" onselectstart="return false"></table>
                                </div>
                            </div>
                        </div>


                    </div>
                </form>
            </div>
        </div>
    </div>
</div>

<script>
    var param_type = getQueryVariable('type') || 'chui';
    $("#type_content button[key='"+param_type+"']").addClass('active');

    var tabHeader;
    layui.use(['laytp', 'form'], function() {
        var form = layui.form, layer = layui.layer;
        render_input_attr();

        $("#type_content button").click(function () {
            var type = $(this).attr('key');
            window.location.href = '/page/main_engin/jian.php?close=1&enginid='+param_enginid+'&type='+type;
        });


        facade.ajax({url: ask_main_jian_engin_list,data:{enginid:param_enginid, type:param_type}}).done(function(res){
            if (res.code === 2000) {
                $("#edittit").html(res.data.tit);

                if(param_type == 'chui'){
                    tabHeader = [
                        {type:'numbers',title:''},
                        {field:'sorts',title:'序号'},
                        {type:'cate',field:'name',title:'名称',required:true},
                        {field:'guige',title:'型号'},
                        {field:'num',title:'数量',type:'number'},
                        {field:'monnum',title:'租赁期(月)',type:'number'},
                        {field:'jinfee',title:'进出场安拆费用',type:'number'},
                        {field:'jinallfee',title:'进出场费合计',type:'number',disabled:true,mathduo:'【jinfee】*【num】'},
                        {field:'jichufee',title:'基础费用',type:'number'},
                        {field:'jichuallfee',title:'基础费用合计',type:'number',disabled:true,mathduo:'【jichufee】*【num】'},
                        {field:'yuzufee',title:'月租金',type:'number'},
                        {field:'zuhefee',title:'租金合计',type:'number',disabled:true,mathduo:'【yuzufee】*【monnum】*【num】'},
                        {field:'zhigong',title:'指挥工资',type:'number'},
                        {field:'zhiren',title:'指挥人数',type:'number'},
                        {field:'sigong',title:'司机工资',type:'number'},
                        {field:'siren',title:'司机人数',type:'number'},
                        {field:'zhihefee',title:'工资合计',type:'number',disabled:true,mathduo:'(【zhigong】*【zhiren】+【sigong】*【siren】)*【monnum】*【num】'},
                        {field:'qifee',title:'其他费用',type:'number'},
                        {field:'allfee',title:'小计',type:'number',disabled:true,mathduo:'【jinallfee】+【jichuallfee】+【zuhefee】+【zhihefee】+【qifee】'},
                        {field:'explain',title:'备注'},
                    ]
                    init({
                        dom:'budtable',
                        header:tabHeader,
                        data: res.data.list,
                        isdeaCate: true,
                        chaNavchild: true,
                        count:['num','monnum','jinfee','jinallfee','jichufee','jichuallfee','yuzufee','zuhefee','zhigong','zhiren','sigong','siren','zhihefee','qifee','allfee'],
                    });
                }

                if(param_type == 'cuo'){
                    tabHeader = [
                        {type:'numbers',title:''},
                        {field:'sorts',title:'序号'},
                        {type:'cate',field:'name',title:'项目名称',required:true},
                        {field:'unit',title:'单位'},
                        {field:'guige',title:'规格'},
                        {field:'num',title:'数量',type:'number'},
                        {field:'price',title:'单价(元)',type:'number'},
                        {field:'tanrate',title:'摊销比例(%)',type:'number',default:100},
                        {field:'heprice',title:'合价(元)',type:'number',mathduo:'【price】*【num】*【tanrate】/100'},
                        {field:'explain',title:'备注'},
                    ]
                    init({
                        dom:'budtable',
                        header:tabHeader,
                        data: res.data.list,
                        isdeaCate: true,
                        chaNavchild: true,
                        count:['num','price','heprice'],
                    });
                }

                if(param_type == 'gong'){
                    tabHeader = [
                        {type:'numbers',title:''},
                        {field:'sorts',title:'序号'},
                        {type:'cate',field:'name',title:'岗位/职务/部门',required:true},
                        {field:'unit',title:'配置人数',type:'number'},
                        {field:'benfee',title:'基本工资',type:'number'},
                        {field:'monnum',title:'月数',type:'number'},
                        {field:'gongfee',title:'公积金(企业)',type:'number'},
                        {field:'shefee',title:'社保(企业)',type:'number'},
                        {field:'hefee',title:'合价',type:'number',mathduo:'(【benfee】+【gongfee】+【shefee】)*【unit】*【monnum】'},
                        {field:'explain',title:'备注'},
                    ]
                    init({
                        dom:'budtable',
                        header:tabHeader,
                        data: res.data.list,
                        isdeaCate: true,
                        chaNavchild: true,
                        count:['unit','benfee','monnum','gongfee','shefee','hefee'],
                    });
                }

                if(param_type == 'guan'){
                    tabHeader = [
                        {type:'numbers',title:''},
                        {field:'sorts',title:'序号'},
                        {type:'cate',field:'name',title:'费用内容',required:true},
                        {field:'unit',title:'单位'},
                        {field:'num',title:'数量',type:'number'},
                        {field:'price',title:'单价(元)',type:'number'},
                        {field:'monnum',title:'月数',type:'number'},
                        {field:'tanrate',title:'摊销比例(%)',type:'number',default:100},
                        {field:'heprice',title:'合价(元)',type:'number',mathduo:'【price】*【num】*【monnum】*【tanrate】/100'},
                        {field:'explain',title:'备注'},
                    ]
                    init({
                        dom:'budtable',
                        header:tabHeader,
                        data: res.data.list,
                        isdeaCate: true,
                        chaNavchild: true,
                        count:['num','price','monnum','heprice'],
                    });
                }

                if(param_type == 'yuone'){
                    tabHeader = [
                        {type:'numbers',title:''},
                        {field:'sorts',title:'序号'},
                        {type:'cate',field:'name',title:'名称',required:true},
                        {field:'unit',title:'单位'},
                        {field:'num',title:'数量',type:'number'},
                        {field:'monnum',title:'时间(月)',type:'number'},
                        {field:'price',title:'单价(元)',type:'number'},
                        {field:'tanrate',title:'摊销比例(%)',type:'number',default:100},
                        {field:'heprice',title:'合价(元)',type:'number',mathduo:'【price】*【num】*【monnum】*【tanrate】/100'},
                        {field:'explain',title:'备注'},
                    ]
                    init({
                        dom:'budtable',
                        header:tabHeader,
                        data: res.data.list,
                        isdeaCate: true,
                        chaNavchild: true,
                        count:['num','price','monnum','heprice'],
                    });
                }

                if(param_type == 'yutwo'){
                    tabHeader = [
                        {type:'numbers',title:''},
                        {field:'sorts',title:'序号'},
                        {type:'cate',field:'name',title:'名称',required:true},
                        {field:'unit',title:'单位'},
                        {field:'num',title:'数量',type:'number'},
                        {field:'monnum',title:'时间(月)',type:'number'},
                        {field:'price',title:'单价(元)',type:'number'},
                        {field:'tanrate',title:'摊销比例(%)',type:'number',default:100},
                        {field:'heprice',title:'合价(元)',type:'number',mathduo:'【price】*【num】*【monnum】*【tanrate】/100'},
                        {field:'explain',title:'备注'},
                    ]
                    init({
                        dom:'budtable',
                        header:tabHeader,
                        data: res.data.list,
                        isdeaCate: true,
                        chaNavchild: true,
                        count:['num','price','monnum','heprice'],
                    });
                }


                render_date();
                render_from();
                initParseMoney();
            }
        });




        form.on('submit(add)', function(data){
            var validRes = $("#commentForm").valid();
            if(!validRes){return false;}

            var listArr = getBudListData();
            try {
                for (key in listArr){
                    var index = parseInt(key) + 1;

                    if(param_type == 'chui'){
                        if(!listArr[key].name){
                            throw new Error('第'+index+'行，请输入名称');
                        }
                    }

                    if(param_type == 'cuo'){
                        if(!listArr[key].name){
                            throw new Error('第'+index+'行，请输入名称');
                        }
                    }

                    if(param_type == 'gong'){
                        if(!listArr[key].name){
                            throw new Error('第'+index+'行，请输入费用内容');
                        }
                    }

                    if(param_type == 'guan'){
                        if(!listArr[key].name){
                            throw new Error('第'+index+'行，请输入岗位/职务/部门');
                        }
                    }
                }
            } catch(e) {
                facade.error(e.message);
                return false;
            }
            data.field.list = listToTree(getBudListData());

            data.field.enginid = param_enginid;
            data.field.type = param_type;
            facade.ajax({url: ask_main_jian_engin_add, data:data.field}).done(function(res){
                if (res.code === 2000) {
                    facade.success("保存成功!");
                    window.location.href = '/page/main_engin/jian.php?close=1&enginid='+param_enginid+'&type='+param_type;
                }
            });

            return false;
        });

    });





</script>
</body>
</html>
